package com.itheima.tlias.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author
 * @version 1.0.1
 * @date 2023-04-11 12:17:18
 */
//交给IOC容器管理
@Component
public class HelloInterceptor implements HandlerInterceptor {
    /**
     * 在Handler执行之前执行，一般用于权限控制
     * @param request
     * @param response
     * @param handler
     * @return true:放行， false:表示拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("HelloInterceptor preHandle");
        return true;
    }

    /**
     * 在Handler执行之后，渲染视图之前执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("HelloInterceptor postHandle");
    }

    /**
     * 所有操作结束之后执行，一般用于释放资源
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("HelloInterceptor afterCompletion");
    }
}
